package com.itheima.Controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j            //生成日志对象
@RestController   //表明这是一个处理请求的类
@RequestMapping("/depts")
public class DeptController {

    @Autowired      //一个才行,0个或多个都不行
    private DeptService deptService ;




    @GetMapping      //声明put请求，路径全部在类上@RequestMapping("/depts")注解表名
        //将返回值响应给客户端,如何返回值是Java对象或集合,会自动转换成json格式
    public Result helloDept() {

        List<Dept> depts = deptService.list();
//        5.返回集合对象
        return Result.success(depts);
    }


    @Log
    @DeleteMapping      //声明delete请求，路径全部在类上@RequestMapping("/depts")注解表名
    public Result delete(Integer id) {
        log.info("根据id删除部门，id={}" , id);

        deptService.delete(id);
        return Result.success();
    }


    @Log
    @PostMapping        //声明post请求，路径全部在类上@RequestMapping("/depts")注解表名
    public Result add(@RequestBody Dept dept){
        log.info("新增部门，dept = {}",dept);

//        阐述传个Service层
        deptService.add(dept);

        return Result.success();
    }



    @GetMapping("/{id}")   //声明get请求，路径全部在类上@RequestMapping("/depts")注解表名
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询部门，id={}",id);


        Dept dept = deptService.getInfo(id);


        return Result.success(dept);
    }

    @Log
    @PutMapping     //声明put请求，路径全部在类上@RequestMapping("/depts")注解表名
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门，dept = {}", dept);

        deptService.update(dept);

        return Result.success();
    }

}
